package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.dto.Simple;
import com.itheima.dto.SubjectsDirectorysDto;
import com.itheima.pojo.SubjectsDirectorys;
import com.itheima.pojo.User;
import com.itheima.service.SubjectsDirectorysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/directorys")
public class SubjectsDirectorysController {

    @Autowired
    private SubjectsDirectorysService subjectsDirectorysService;

    @Autowired
    private HttpServletRequest req;

    /**
     *
     * Description:
     *目录修改
     * @param:  * @param id
     * @param subjectsDirectorys
     * @return: {@link String}
     */
    @PutMapping("/{id}")
    public String modifySubjectsDirectorysById(@PathVariable Integer id, @RequestBody SubjectsDirectorys subjectsDirectorys){
        subjectsDirectorysService.modifySubjectsDirectorysById(subjectsDirectorys);
        return "{'success':true}";
    }

    /**
     *
     * Description:
     *目录添加
     * @param:  * @param subjectsDirectorys
     * @return: {@link String}
     */
    @PostMapping
    public String addSubjectsDirectorys(@RequestBody SubjectsDirectorys subjectsDirectorys){
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        subjectsDirectorys.setCreatorID(user.getId());
        subjectsDirectorys.setAddDate(LocalDateTime.now());
        Integer id = subjectsDirectorysService.addSubjectsDirectorys(subjectsDirectorys);

        return "{'id':"+id+"}";
    }


    /**
     *
     * Description:
     *目录列表
     * @param:  * @param page
     * @param pagesize
     * @param subjectID
     * @param directoryName
     * @param state
     * @return: {@link PageResult}
     */
    @GetMapping
    public PageResult subjectsDirectorysPage(Integer page, Integer pagesize, Integer subjectID, String directoryName, Integer state){
        return subjectsDirectorysService.subjectsDirectorysPage(page,pagesize,subjectID,directoryName,state);
    }

    /**
     *
     * Description:
     *目录删除
     * @param:  * @param id
     * @param subjectsDirectorys
     * @return: {@link String}
     */
    @DeleteMapping("/{id}")
    public String removeSubjectsDirectorysById(@PathVariable Integer id,@RequestBody SubjectsDirectorys subjectsDirectorys){
        subjectsDirectorysService.removeSubjectsDirectorysById(id);

        return "{'success':false}";
    }
    /**
     *
     * Description:
     *目录修改
     * @param:  * @param id
     * @param state
     * @return: {@link String}
     */
    @PostMapping("/{id}/{state}")
    public String modifySubjectsDirectorysState(@PathVariable Integer id,@PathVariable Integer state){
        subjectsDirectorysService.modifySubjectsDirectorysState(id,state);
        return "{'success':true}";

    }

    /**
     *
     * Description:
     *目录详情
     * @param:  * @param id
     * @return: {@link SubjectsDirectorys}
     */
    @GetMapping("/{id}")
    public SubjectsDirectorysDto findSubjectsDirectorysById(@PathVariable Integer id){
        return subjectsDirectorysService.findSubjectsDirectorysById(id);
    }


    @GetMapping("/simple")
    public List<Simple> subjectsDirectorysSimple(Integer subjectID){
       return subjectsDirectorysService.subjectsDirectorysSimple(subjectID);
    }
}
